package leetcode_bin_tree_test;

import bin_tree.TreeNode;

/**
 * 路径总和
 */
public class PathCombined {
    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        targetSum -= root.val;
        // 条件是该节点没有左右子树，且到该节点时targetSum已经全部找到
        if (targetSum == 0 && root.left == null && root.right == null) {
            return true;
        }
        return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
//        TreeNode right = new TreeNode(3);
        root.left = left;
//        root.right = right;
        int target = 1;
        System.out.println(hasPathSum(root, target));
    }
}
